package com.triptogether.utils.db;

import java.util.Calendar;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.triptogether.utils.db.models.Message;
import com.triptogether.utils.db.models.Product;
import com.triptogether.utils.db.models.ShoppingList;
import com.triptogether.utils.db.models.Trip;
import com.triptogether.utils.db.models.User;
import com.triptogether.utils.db.models.UsersToTrip;
import com.triptogether.utils.log.Logger;

public class DBHelper extends OrmLiteSqliteOpenHelper {
	private String TAG = Logger.makeLogTag(getClass());

	// name of the database file for your application
	private static final String DATABASE_NAME = "TripTogetherDB.sqlite";

	// any time you make changes to your database objects, you may have to
	// increase the database version
	private static final int DATABASE_VERSION = 1;

	private Dao<User, Long> usersDao = null;
	private Dao<Message, Long> messagesDao = null;
	private Dao<Product, Long> productsDao = null;
	private Dao<Trip, Long> tripDao = null;
	private Dao<ShoppingList, Long> shoppingListDao = null;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, User.class);
			TableUtils.createTable(connectionSource, Trip.class);
			TableUtils.createTable(connectionSource, Message.class);
			TableUtils.createTable(connectionSource, Product.class);
			TableUtils.createTable(connectionSource, ShoppingList.class);
			TableUtils.createTable(connectionSource, UsersToTrip.class);
			
			tempAddTrip("gal");
			tempAddTrip("trip one");
			tempAddTrip("tal");
			tempAddTrip("inbar <3");
			tempAddTrip("trip 3");
			
		} catch (SQLException e) {
			Logger.LOGE(TAG, e.getMessage());
			throw new RuntimeException(e);
		} catch (java.sql.SQLException e) {
			Logger.LOGE(TAG, e.getMessage());
		}

	}
	
	// TODO: delete this
	private void tempAddTrip(String tripName) {
		Trip trip = new Trip();
		trip.setTripName(tripName);
		trip.setTripDate(Calendar.getInstance().getTime());
		trip.setDateCreated(Calendar.getInstance().getTime());
		try {
			getTripDao().create(trip);
		} catch (java.sql.SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {}

	public Dao<User, Long> getUsersDao() {
		if (this.usersDao == null) {
			try {
				this.usersDao = getDao(User.class);
			} catch (Exception e) {
				Logger.LOGE(TAG, e.getMessage());
			}
		}
		return this.usersDao;
	}

	public Dao<Message, Long> getMessagesDao() {
		if (this.messagesDao == null) {
			try {
				this.messagesDao = getDao(Message.class);
			} catch (Exception e) {
				Logger.LOGE(TAG, e.getMessage());
			}
		}
		return this.messagesDao;
	}
	
	public Dao<Product, Long> getProductsDao() {
		if (this.productsDao == null) {
			try {
				this.productsDao = getDao(Product.class);
			} catch (Exception e) {
				Logger.LOGE(TAG, e.getMessage());
			}
		}
		return this.productsDao;
	}
	
	public Dao<ShoppingList, Long> getShoppingListDao() {
		if (this.shoppingListDao == null) {
			try {
				this.shoppingListDao = getDao(ShoppingList.class);
			} catch (Exception e) {
				Logger.LOGE(TAG, e.getMessage());
			}
		}
		return this.shoppingListDao;
	}
	
	public Dao<Trip, Long> getTripDao() {
		if (this.tripDao == null) {
			try {
				this.tripDao = getDao(Trip.class);
			} catch (Exception e) {
				Logger.LOGE(TAG, e.getMessage());
			}
		}
		return this.tripDao;
	}
}
